package com.wanshi.servlet;

import com.wanshi.entity.PageInfo;
import com.wanshi.entity.User;
import com.wanshi.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@WebServlet("/pageByCond")
public class PageCondServlet extends HttpServlet {
    private UserService userService = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //查询第几页  每页显示多少条数据
        int page = Integer.parseInt(request.getParameter("page"))  ;
        int size =  Integer.parseInt(request.getParameter("size")) ;

        User user = new User();
        //前提 表单中表单元素的name属性值 和 对象的属性一致
        try {
            BeanUtils.copyProperties(user,request.getParameterMap());

            PageInfo pageInfo = userService.findByPageAndCond(page,size,user);
            request.setAttribute("page",pageInfo);
            request.getRequestDispatcher("/pageCond.jsp").forward(request,response);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

